extends Area2D

var SPEED = 800
var direction = Vector2.UP  # 默认向上移动

func _ready():
	add_to_group("player_bullet")  # 将子弹添加到 player_bullet 组
	
func _physics_process(delta):
	position += direction * SPEED * delta
	
	# 检查子弹是否超出屏幕
	var viewport_size = get_viewport_rect().size
	if position.y < -10 or position.y > viewport_size.y + 10 or \
	   position.x < -10 or position.x > viewport_size.x + 10:
		queue_free()

func _on_area_entered(area: Area2D) -> void:
	# 如果是玩家子弹击中敌人
	if is_in_group("player_bullet") and area.is_in_group("enemy"):
		queue_free()
	
	# 如果是敌人子弹击中玩家
	elif is_in_group("enemy_bullet") and area.is_in_group("player"):
		var main_player = get_tree().get_nodes_in_group("player")[0]
		if main_player:
			main_player.game_over()
		queue_free()
